﻿// AmiResponseMessage.cs
// Copyright © 2013 Alex Forster

namespace Ami
{
	using System;
	using System.Diagnostics.Contracts;

	public sealed class AmiResponseMessage: AmiMessage
	{
		public new static MessageConverter Converter;

		static AmiResponseMessage()
		{
			AmiMessage.Converter = AmiResponseMessage.ResponseConverter;
		}

		public DateTime Sent
		{
			get;
			private set;
		}

		private static AmiMessage ResponseConverter( AmiMessage message )
		{
			Contract.Assert( message != null );

			if( message["Response"] == null )
			{
				return null;
			}

			var result = new AmiResponseMessage();

			result.Headers = message.Headers;

			var actionMilliseconds = Convert.ToInt64( result.ActionID.Split( ';' )[0], 16 );

			result.Sent = new DateTime( 1970, 1, 1, 0, 0, 0, DateTimeKind.Utc )
				.AddMilliseconds( actionMilliseconds );

			result.Received = DateTime.UtcNow;

			return result;
		}

		public String Response
		{
			get { return this["Response"]; }
		}

		public String ActionID
		{
			get { return this["ActionID"]; }
		}
	}
}
